<?php

$export = new Export();
$export->exportDocs();

class Export
{
    protected $_savePath;
    
    protected $_userCsvPath;
    
    const OAUTHKEY = '';
    
    const GDATA_API = '';
    
    const DOMAIN_APPEND = '@ncsu.edu';
    
    protected $_users = array();
    
    protected $_complete = array();
    
    /*
     * Connect to google api
     */
    public function __construct()
    {
        $this->_savePath = APPLICATION_PATH . '/users';
        $this->_userCsvPath = APPLICATION_PATH . '/users.csv';
    }
    
    public function exportDocs()
    {
        set_time_limit(0);
        
        try {
            $this->_importUsers();
            
            foreach ($this->_users as $u) {
                $this->_getDocsForUser($u);
                $this->_complete[] = $u;
            }
        } catch (Exception $e) {
            $this->_die($e);
        }
    }
    
    protected function _die(Exception $e)
    {
        echo count($this->_complete) . " Records complete before error.\n\n";
        if (count($this->_complete) > 0) {
            echo "Last successful user was " . $this->_complete[count($this->_complete) - 1];
        } else  {
            echo "No user exports were successful";
        }
        echo "\n\n";
        echo "Exception: " . $e->getMessage();
        die();
    }
    
    protected function _importUsers()
    {
        $row = 1;
        if (($handle = fopen($this->_userCsvPath, "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                if ($row != 1) {
                    $user = $data[0];
                    
                    $this->_createFolderForUser($user . self::DOMAIN_APPEND);
                    $this->_users[] = $user;
                }
                $row++;
            }
            fclose($handle);
        }
    }
    
    protected function _createFolderForUser($user)
    {
        if (!is_dir($this->_savePath . '/' . $user)) {
            if (!mkdir($this->_savePath . '/' . $user)) {
                throw new Exception('PATH can not be created for ' . $user);
            }
        }
    }
    
    protected function _writeDocToFile($filename, $fileextension, $filecontents, $user)
    {
        
        $filename = str_replace('/', '-', $filename);
        $filename = urlencode($filename);
        
        $path = $this->_savePath . '/' . $user . self::DOMAIN_APPEND . '/' . $filename . '.' . $fileextension;
        
        if (!is_file($path)) {
            if (file_put_contents($path, $filecontents) === false) {
                throw new Exception($filename . '.' . $fileextension . ' for ' . $user . ' was not written');
            };
        }
    }
    
    protected function _getDocsForUser($user)
    {
        // the list of user's docs
        $docs = new Gdata_Apps_Docs();
        $docList = $docs->getDocsList($user);
        
        foreach ($docList as $d) {
            $result = $docs->download($d['user'], $d['docId'], $d['type']);
            
            $this->_writeDocToFile($d['title'], $result['extension'], $result['contents'], $user);
        }
    }
}